package com.hqyj.controller;

import com.hqyj.utile.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Random;


@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    EmailUtil emailUtil;

    //发送验证码
    @RequestMapping("/sendCode")
    public HashMap<String,Object> sendCode(String email, HttpSession session){
        HashMap<String,Object> map = new HashMap<String,Object>();
        //随机对象
        Random rd = new Random();
        int code =rd.nextInt(1000);
        System.out.println(email);
        String info = emailUtil.sendEmail(email,"酒店公司","你的验证码是"+code);
        if(info.equals("success")){
            map.put("info","验证码发送成功");
            session.setAttribute("code",code);
            session.setMaxInactiveInterval(60000);
        }else{
            map.put("info","验证码发送失败");
        }
        return map;
    }

    //邮箱登录
    @RequestMapping("/emailLogin")
    public HashMap<String,Object> emailLogin(String code, HttpSession session){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String sessionCode = session.getAttribute("code")+"";
        if(code.equals(sessionCode)){
            map.put("info","邮箱登录成功");
        }else{
            map.put("info","邮箱登录失败");
        }
        return map;
    }
}
